package itsm.isperp.module.controller.api.wechat;

import itsm.isperp.framework.data.domain.DataResponse;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.serviceCatalog.SrcService;
import itsm.isperp.module.service.serviceCatalog.SrcCategoryService;
import itsm.isperp.module.service.serviceCatalog.SrcServiceService;

import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * 
 * 
 * @author lizx
 * @date 2014-3-15
 * @since 1.0
 * 
 */
@Controller
@RequestMapping("api/wechat/serviceCatalog")
public class WechatServiceCatalogController {

	@Autowired
	SrcCategoryService srcCategoryService;

	@Autowired
	SrcServiceService srcServiceService;

	@RequestMapping(value = { "", "category" })
	public ModelAndView category() {
		ModelAndView mv = new ModelAndView("api/wechat/serviceCatalog/category");
		JqGridRequest request = new JqGridRequest();
		request.isPageable(false);
		Object o = srcCategoryService.findAll(request);
		mv.addObject("result", o);
		return mv;
	}

	@RequestMapping("service")
	public ModelAndView service(String category, String cateName)
			throws SQLException {

		ModelAndView mv = new ModelAndView("api/wechat/serviceCatalog/service");
		JqGridRequest request1 = new JqGridRequest();
		request1.isPageable(false);
		request1.setSort("order_num",Direction.ASC);
		request1.setSearchString(" EXISTS(SELECT 1 FROM src_service  WHERE category_id=src_category.id)");
		Object o = srcCategoryService.findAll(request1);
    mv.addObject("cateList", o);
    JqGridRequest request2=new JqGridRequest();
    request2.isPageable(false);
    request2.addFilterRule("status", JqGridFilterRuleOp.eq, "发布");
    request2.setSort("category_id", Direction.ASC);
    request2.setSelectColumns("id", "name", "keyword", "service_way","category_id","photo");
		DataResponse<SrcService> serviceList=srcServiceService.findAll(request2);
		mv.addObject("serviceList", serviceList);
		return mv;
	}

	@RequestMapping("detail")
	public ModelAndView detail(String id) throws SQLException {

		ModelAndView mv = new ModelAndView("api/wechat/serviceCatalog/detail");

		mv.addObject("model", srcServiceService.findOneDetailById(id));

		return mv;
	}
}
